package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.user.OrderService;
import com.sky.utils.JwtUtil;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.Alias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user/order")
@Slf4j
@Api(tags = "c端订单模块对应接口")
@Alias("userOrderController")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("submit")
    @ApiOperation("用户下单对应接口")
    @CacheEvict(cacheNames = "orderHistory" ,allEntries = true )
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO dto, HttpServletRequest request) {
        Result<OrderSubmitVO> result = orderService.submitOrder(dto);
        return result;
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);

//        return "redirect:/static/index.html";

        return Result.success(orderPaymentVO);
    }

    @GetMapping("historyOrders")
    @ApiOperation("用户查看订单历史记录对应接口")
    public Result<PageResult> getUserOrderHistory(OrdersPageQueryDTO dto){
        Long userId = BaseContext.getCurrentId();
        Result<PageResult> result = orderService.getUserOrderHistory(dto,userId);
        return result;
    }

    @GetMapping("orderDetail/{id}")
    @ApiOperation("用户查看历史订单详情的对应接口")
    public Result<Orders> getOrderHistoryDetail(@PathVariable Long id){
           Result<Orders> result =  orderService.getOrderHistoryDetailById(id);
           return result;
    }

    @PutMapping("cancel/{id}")
    @ApiOperation("用户取消订单对应接口")
    @CacheEvict(cacheNames = "orderHistory" ,allEntries = true )
    public Result<Object> cancelOrder(@PathVariable Long id){
        log.info("用户取消订单id:{}",id);
        Result<Object> result = orderService.cancelOrder(id);
        return result;
    }

    @PostMapping("repetition/{id}")
    @ApiOperation("用户端再来一单对应接口")
    @CacheEvict(cacheNames = "orderHistory" ,allEntries = true )
    public Result<Object> repetitionOrder(@PathVariable Long id){
        log.info("用户再来一单订单id:{}",id);

        Result<Object> result = orderService.repetitionOrderById(id);

        return result;
    }

    @GetMapping("reminder/{id}")
    @ApiOperation("用户端催单对应接口")
    public Result<Object> reminderOrder(@PathVariable Long id){
        orderService.reminderOrderById(id);

        return Result.success();
    }



}
